﻿using FW.Manager;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListenerRequest request;
            HttpListenerResponse response;
            string action;
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:3535/");
            Console.WriteLine("Http Listener started");
            byte[] buffer;
            string html = "<a onclick=\"javascript:alert('selam');\"> BAsınız </a>";
            string wellcomeHtml = "<h1 style='color:red'> WellCome {0} </h1>";
            string errorHtml = "<h1 style='color:red'> WellCome {0} </h1>";
            string login = "<form action='login.fatih' method='POST' id='frm'><h1> Wellcome </h1><div><input id='username' name='username' type='text'/><input id='pass' name='pass' type='password'/><input type='submit' value='Login' /> </div></form>";
            string postedFormString;
            StreamReader sr;
            listener.Start();
            while (true)
            {
                HttpListenerContext context = listener.GetContext();
                request = context.Request;
                response = context.Response;
                response.ContentType = "text/html";
                if (request.Url.AbsolutePath.Contains("login.fatih"))
                {
                    if (request.HttpMethod == "POST")
                    {
                        using (sr = new StreamReader(request.InputStream))
                        {
                            postedFormString = sr.ReadToEnd();
                            string[] postedValues = postedFormString.Split('&');
                            if (postedValues.Length >= 2)
                            {
                                string userName = postedValues[0].Substring(postedValues[0].LastIndexOf("=")+1);
                                string pass = postedValues[1].Substring(postedValues[1].LastIndexOf("=")+1);

                                if (userName == "fatih" && pass == "123")
                                {
                                    buffer = System.Text.Encoding.ASCII.GetBytes(string.Format(wellcomeHtml, userName));
                                    response.StatusCode = 200;
                                    response.OutputStream.Write(buffer, 0, buffer.Length);
                                    response.OutputStream.Dispose();
                                }
                                else
                                {
                                    buffer = System.Text.Encoding.ASCII.GetBytes(login+"<script> alert('hata'); </script>");
                                    response.StatusCode = 200;
                                    response.OutputStream.Write(buffer, 0, buffer.Length);
                                    response.OutputStream.Dispose();
                                }
                            }
                        }
                    }
                    else
                    {
                        buffer = System.Text.Encoding.ASCII.GetBytes(login);
                        response.StatusCode = 200;
                        response.OutputStream.Write(buffer, 0, buffer.Length);
                        response.OutputStream.Dispose();
                    }
                }


            }

            Console.ReadLine();
        }
    }
}
